(function() {
    
    
        run();             //加载页面时启动定时器  
       var interval;  
       function run() {  
          interval = setInterval(get_notice_count, "3000");  
       }  
       function get_notice_count() {  
           $.ajax({
              type: 'POST',
              url: getCookie('CakeCookie[path]')+'msgs/ajax_get_count',
              dataType:'json',
              success: function(data){
           //  alert(data);
                
                  /* if(!data){
                       window.history.go(0);
                      return false;
                   }                       
                     */ 
                  if(data != 0 || data != "0"){
                  
                    $("#p_m_alert").empty();
                    $("#p_m_alert").show();
                   
                    $("#p_m_alert").html(data);
                      return false;
                  }
                    
                     
                      
                      
                }
            });  
       }  
    
    //上传
    var ajax_upload_file = function(fieldClass,targetClass) {
        var file = new File();
        file.uploadFile(fieldClass,targetClass);
    };
   
   window.ajax_upload_file = ajax_upload_file;
    
   //删除
    var delete_ajax_file = function(obj) {
    
        var file = new File(obj.id);
        file.deleteFile();
        
        obj.remove();
    };
   window.delete_ajax_file = delete_ajax_file;
    
   //检查已上传文件个数
    var checkFileCount = function(){
    
        if($("[name='upload[]']:hidden").length >=5){
            alert('最多上传5个附件');
            return false;
        }
    }
    window.checkFileCount = checkFileCount;
    
    var setCookie = function(c_name,value,expiredays)
    {
        var exdate=new Date()
        exdate.setDate(exdate.getDate()+expiredays)
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
    }
    
    window.setCookie = setCookie;
    
    var getCookie = function(c_name)
    {
        if (document.cookie.length>0)
          {
          c_start=document.cookie.indexOf(c_name + "=")
          if (c_start!=-1)
            { 
            c_start=c_start + c_name.length+1 
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end))
            } 
          }
        return ""
    }
    
    window.getCookie = getCookie;
}) ();


//文件
var File = function(fileId) {
   this.fileName = '';
   this.filePath = '';
   this.fileId = fileId;
   
};

File.prototype.uploadFile = function(fieldClass,targetClass) {
    
    var fileField = new FileField(fieldClass);
    var targetField = new TargetField(targetClass);
    
    fileField.createForm();
    
    $("."+fieldClass).change(function(){
        var options = {
            dataType: 'json',
//            beforeSubmit: checkFileCount,
            success: function(data){
               targetField.addFile(data);
            }
        };
        $('.'+fieldClass+'Form').ajaxSubmit(options);
    });
    
};



File.prototype.deleteFile = function() {
    
    $("[name='upload[]'][value='"+this.fileId+"']:hidden").remove();
};

//表单上传文件字段
var FileField = function(fieldClass) {
    
   this.fieldClass = fieldClass;

};

FileField.prototype.urlPath = getCookie('CakeCookie[path]')+'msgs/upload';

//创建表单
FileField.prototype.createForm = function(){
    
    $("."+this.fieldClass).wrap("<form action='"+this.urlPath+"' method='post'  enctype='multipart/form-data' class='"+this.fieldClass+"Form'></form>");
    
    $("."+this.fieldClass).attr("name","upload");
   
};

//返回结果
var TargetField = function(targetClass) {
    
   this.targetClass = targetClass;
 
};
//添加文件
TargetField.prototype.addFile = function(data){
    
    $("."+this.targetClass).append("<a href='javascript:;' onclick=\"delete_ajax_file(this)\" id='"+data.file_id+"'>"+data.file_name+" ; </a>"); 
    $("."+this.targetClass).append("<input type='hidden' name='upload[]' value='"+data.file_id+"'>"); 
};





